package ldh.im.monitor.rpc;

import ldh.im.dto.business.item.MonitorUserInfo;
import ldh.im.dto.business.monitor.MonitorActiveUserInfo;
import ldh.im.dto.business.monitor.MonitorActiveUserSummary;
import ldh.im.dto.business.monitor.MonitorSummaryInfo;
import ldh.im.fxbase.data.event.EventBusFactory;
import ldh.im.fxbase.util.RpcUtil;
import ldh.im.monitor.event.MonitorActiveUserCollection;
import ldh.im.monitor.util.Param;
import ldh.rpc.util.JsonUtil;
import ldh.im.util.Sets;
import ldh.rpc.Rpc;
import ldh.rpc.RpcMapper;
import ldh.rpc.cache.CacheService;
import ldh.rpc.exception.RpcException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

@Rpc
public class RpcMonitorRouter {

    private static final Logger LOGGER = LoggerFactory.getLogger(RpcMonitorRouter.class);

    /**
     * 接受汇总信息
     * @param monitorSummaryInfo
     * @throws RpcException
     */
    @RpcMapper(path = "summaryInfo")
    public void receiveSummaryInfo(MonitorSummaryInfo monitorSummaryInfo) throws RpcException {
        LOGGER.info("receiveSummaryInfo {}", JsonUtil.toJson(monitorSummaryInfo));

        cacheMonitorSummaryInfo(monitorSummaryInfo);

        EventBusFactory.getInstance().post(monitorSummaryInfo);
    }

    /**
     * 接受汇总信息 + 登录用户信息
     * @param monitorActiveUserSummary
     * @throws RpcException
     */
    @RpcMapper(path = "userSummaryInfo")
    public void receiveSummaryInfo(MonitorActiveUserSummary monitorActiveUserSummary) throws RpcException {
        LOGGER.info("receiveSummaryInfo {}", JsonUtil.toJson(monitorActiveUserSummary));

        Set<MonitorActiveUserInfo> monitorActiveUserInfos = monitorActiveUserSummary.getMonitorUserInfoList().stream().map(this::buildMonitorActiveUserInfo)
                .collect(Collectors.toSet());
        cacheMonitorActiveUserInfo(monitorActiveUserInfos);
        EventBusFactory.getInstance().post(new MonitorActiveUserCollection(monitorActiveUserInfos));


        MonitorSummaryInfo monitorSummaryInfo = new MonitorSummaryInfo();
        monitorSummaryInfo.setTotal(monitorActiveUserSummary.getTotal());
        monitorSummaryInfo.setConnectionTotal(monitorActiveUserSummary.getConnectionTotal());
        monitorSummaryInfo.setConnectionTotalMap(monitorActiveUserSummary.getConnectionTotalMap());
        cacheMonitorSummaryInfo(monitorSummaryInfo);

        EventBusFactory.getInstance().post(monitorSummaryInfo);
    }

    /**
     * 接受用户登录信息
     * @param monitorActiveUserInfo
     * @throws RpcException
     */
    @RpcMapper(path = "userLoginInfo")
    public void receiveUserLoginInfo(MonitorActiveUserInfo monitorActiveUserInfo) throws RpcException {
        LOGGER.info("receiveUserLoginInfo {}", JsonUtil.toJson(monitorActiveUserInfo));
        cacheMonitorActiveUserInfo(Sets.asSet(monitorActiveUserInfo));
        EventBusFactory.getInstance().post(monitorActiveUserInfo);
    }

    /**
     * 接受所有用户登录信息
     * @param monitorActiveUserList
     * @throws RpcException
     */
    @RpcMapper(path = "userLoginInfoes")
    public void receiveUserLoginInfoes(MonitorActiveUserCollection monitorActiveUserList) throws RpcException {
        LOGGER.info("receiveUserLoginInfo {}", JsonUtil.toJson(monitorActiveUserList));
        cacheMonitorActiveUserInfo(monitorActiveUserList.getMonitorActiveUserInfoSet());

        EventBusFactory.getInstance().post(new MonitorActiveUserCollection(monitorActiveUserList.getMonitorActiveUserInfoSet()));
    }

    private void cacheMonitorSummaryInfo(MonitorSummaryInfo monitorSummaryInfo) {
        CacheService cacheService = RpcUtil.getRpcBootstrap().getRpcConfig().getCacheService();
        cacheService.setCache(Param.CACHE_MONITOR_SUMMARY, monitorSummaryInfo, null);
    }

    private void cacheMonitorActiveUserInfo(Set<MonitorActiveUserInfo> monitorActiveUserInfoes) {
        CacheService cacheService = RpcUtil.getRpcBootstrap().getRpcConfig().getCacheService();
        Set<MonitorActiveUserInfo> monitorActiveUserInfoList = (Set<MonitorActiveUserInfo>) cacheService.getCache("", Param.CACHE_USER_LIST);
        if (monitorActiveUserInfoList == null) {
            monitorActiveUserInfoList = new LinkedHashSet<>();
            cacheService.setCache(Param.CACHE_USER_LIST, monitorActiveUserInfoList, null);
        }
        monitorActiveUserInfoList.addAll(monitorActiveUserInfoes);
    }

    private MonitorActiveUserInfo buildMonitorActiveUserInfo(MonitorUserInfo monitorUserInfo) {
        MonitorActiveUserInfo monitorActiveUserInfo = new MonitorActiveUserInfo();
        monitorActiveUserInfo.setMonitorUserInfo(monitorUserInfo);
        return monitorActiveUserInfo;
    }

}
